﻿using System.ComponentModel;
using System.Drawing.Design;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using Rsdn.Janus.Framework;

namespace Rsdn.Janus
{
    /// <summary>
    /// Настройки звуков нотификации.
    /// </summary>
    public class SoundConfig : SubConfigBase
    {
        private const bool _defaultMakeSound = true;
        private bool _makeSound = _defaultMakeSound;

        [JanusDisplayName(SR.Config.Sound.Use.DisplayNameResourceName)]
        [JanusDescription(SR.Config.Sound.Use.DescriptionResourceName)]
        [DefaultValue(_defaultMakeSound)]
        [SortIndex(5)]
        public bool MakeSound
        {
            get { return _makeSound; }
            set { _makeSound = value; }
        }

        private const string _defaultSoundFile1 = @"Sound\Alarm.wav";
        private string _soundFile1 = _defaultSoundFile1;

        [JanusDisplayName(SR.Config.Sound.File1.DisplayNameResourceName)]
        [JanusDescription(SR.Config.Sound.File1.DescriptionResourceName)]
        [Editor(typeof(WavFileEditor), typeof(UITypeEditor))]
        [DefaultValue(_defaultSoundFile1)]
        [SortIndex(6)]
        public string SoundFile1
        {
            get { return _soundFile1; }
            set { _soundFile1 = value; }
        }

        private const string _defaultSoundFile2 = @"Sound\Bum.wav";
        private string _soundFile2 = _defaultSoundFile2;

        [JanusDisplayName(SR.Config.Sound.File2.DisplayNameResourceName)]
        [JanusDescription(SR.Config.Sound.File2.DescriptionResourceName)]
        [Editor(typeof(WavFileEditor), typeof(UITypeEditor))]
        [DefaultValue(_defaultSoundFile2)]
        [SortIndex(7)]
        public string SoundFile2
        {
            get { return _soundFile2; }
            set { _soundFile2 = value; }
        }

        private const string _defaultSoundFile3 = @"Sound\Click.wav";
        private string _soundFile3 = _defaultSoundFile3;

        [JanusDisplayName(SR.Config.Sound.File3.DisplayNameResourceName)]
        [JanusDescription(SR.Config.Sound.File3.DescriptionResourceName)]
        [Editor(typeof(WavFileEditor), typeof(UITypeEditor))]
        [DefaultValue(_defaultSoundFile3)]
        [SortIndex(8)]
        public string SoundFile3
        {
            get { return _soundFile3; }
            set { _soundFile3 = value; }
        }

        private class WavFileEditor : FileNameEditor
        {
            public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
            {
                Keys keys = Control.ModifierKeys;
                if (keys == Keys.Control)
                {
                    if (value is string)
                    {
                        Beeper.DoBeep((string)value);
                    }
                }
                else
                {
                    return base.EditValue(context, provider, value);
                }

                return value;
            }

            protected override void InitializeDialog(OpenFileDialog ofd)
            {
                ofd.Filter = "Wav file (*.wav)|*.wav";
            }
        }
    }
}
